package com.leostadyn.stackfarm.timestatistics.view.activity.admin.staff;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kaopiz.kprogresshud.KProgressHUD;
import com.leostadyn.stackfarm.timestatistics.R;
import com.leostadyn.stackfarm.timestatistics.adapter.StaffListAdapter;
import com.leostadyn.stackfarm.timestatistics.base.BaseActivity;
import com.leostadyn.stackfarm.timestatistics.common.Constants;
import com.leostadyn.stackfarm.timestatistics.model.bean.DeleteBean;
import com.leostadyn.stackfarm.timestatistics.model.bean.UserListBean;
import com.leostadyn.stackfarm.timestatistics.net.api.AdminApi;
import com.leostadyn.stackfarm.timestatistics.net.base.OriginalSubscriber;
import com.leostadyn.stackfarm.timestatistics.net.base.ResponseThrowable;
import com.leostadyn.stackfarm.timestatistics.utils.MyDialog;
import com.leostadyn.stackfarm.timestatistics.utils.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

public class StaffListActivity extends BaseActivity implements View.OnClickListener {

    private SmartRefreshLayout smartRefreshLayout;
    private TextView nav_title, add;
    private RecyclerView rv_staff;
    private LinearLayout nodata_layout;
    private List<UserListBean> userBeanList = new ArrayList<>();
    private StaffListAdapter staffListAdapter;
    private HashMap<Integer,Object> mPosion=new HashMap<>();
    private ImageView nav_back;
    private int mPage = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_people_list);
        initView();

    }

    public void initView()
    {
        rv_staff=findViewById(R.id.rv_staff);
        smartRefreshLayout=findViewById(R.id.srl_area);
        nav_title=findViewById(R.id.nav_txt_title);
        nav_title.setText("人员列表");
        nodata_layout = findViewById(R.id.layout_no_data);
        nav_back = findViewById(R.id.nav_img_back);
        add = findViewById(R.id.nav_btn_add);
        add.setVisibility(View.VISIBLE);
        add.setOnClickListener(this);
        nav_back.setOnClickListener(this);

        //对recyclerview无数据的UI展示


        rv_staff.setLayoutManager(new LinearLayoutManager(this));
        staffListAdapter=new StaffListAdapter(this, userBeanList, mPosion, new StaffListAdapter.OnDeleteButtonClick() {

            @Override
            public void onBtnClicked(int position, UserListBean data, TextView name, TextView job, TextView id, TextView time) {
                dialogShow(data);

            }
        });

        rv_staff.setAdapter(staffListAdapter);
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPage = 1;
                onStaffList();
            }
        });
        smartRefreshLayout.setOnLoadMoreListener(refreshLayout -> {
                onStaffList();
        });

        onStaffList();

    }
    public static final int  REQUESTCODE=100;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==REQUESTCODE&&resultCode== Activity.RESULT_OK) {
            mPage = 1;
            onStaffList();
        }
    }

    //调用接口请求数据并返回
    public void onStaffList(){
        HashMap<String, String> mapQuery = new HashMap<String, String>();

        mapQuery.put("page", String.valueOf(mPage));
        mapQuery.put("pageSize", String.valueOf(Constants.PAGESIZE));

        final KProgressHUD dialog = KProgressHUD.create(this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setCancellable(true).setAnimationSpeed(2).setDimAmount(0.5f);
        dialog.show();

        AdminApi.getInstance().getStaffList(mapQuery).subscribe(new OriginalSubscriber<List<UserListBean>>(compositeDisposable) {
            @Override
            public void onError(ResponseThrowable e) {
                dialog.dismiss();
                ToastUtil.show(StaffListActivity.this, e.getMsg());
            }

            @Override
            public void onNext(List<UserListBean> userListBeans, String msg) {
                if (smartRefreshLayout != null) {
                    smartRefreshLayout.finishLoadMore();
                    smartRefreshLayout.finishRefresh();
                }
                dialog.dismiss();
                if (mPage == 1) {
                    if (userListBeans != null)
                        Collections.sort(userListBeans, Collections.reverseOrder());
                    smartRefreshLayout.setEnableLoadMore(true);
                    staffListAdapter.setStaffListBeans(userListBeans);
                    if (userListBeans == null || userListBeans.size() == 0) {
                        nodata_layout.setVisibility(View.VISIBLE);
                        rv_staff.setVisibility(View.GONE);
                    } else {
                        nodata_layout.setVisibility(View.GONE);
                        rv_staff.setVisibility(View.VISIBLE);
                    }
                } else {
                    staffListAdapter.setStaffListBeans(userListBeans);
                }
                if (userListBeans != null) {
                    if (userListBeans.size() < Constants.PAGESIZE) {
                        smartRefreshLayout.setEnableLoadMore(false);
                    } else {
                        mPage++;
                    }
                }
            }

        });



    }



    public void dialogShow(UserListBean data)
    {
        View view = getLayoutInflater().inflate(R.layout.layout_dialog, null);
        TextView dialog_delete=view.findViewById(R.id.dialog_delete);
        TextView dialog_cancel=view.findViewById(R.id.dialog_cancel);

        final Dialog dialog=new MyDialog(StaffListActivity.this,0,0,view,R.style.DialogTheme);
        dialog.setCancelable(true);
        dialog.show();

        dialog_delete.setOnClickListener(v -> {
            deleteData(data.getUid());
            dialog.dismiss();

        });
        dialog_cancel.setOnClickListener(v -> {
            dialog.dismiss();
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.nav_img_back:
                StaffListActivity.this.finish();
                break;
            case R.id.nav_btn_add:
                startActivity(new Intent(this, StaffAddActivity.class));
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        onStaffList();
    }

    public void deleteData(String id) {
        HashMap<String, String> deletemap = new HashMap<>();
        deletemap.put("uid", id);

        AdminApi.getInstance().deleteData(deletemap).subscribe(new OriginalSubscriber<DeleteBean>(compositeDisposable) {
            @Override
            public void onError(ResponseThrowable e) {
                ToastUtil.show(StaffListActivity.this, "该项目有工时，无法删除！");
            }

            @Override
            public void onNext(DeleteBean deleteBean, String msg) {
                ToastUtil.show(StaffListActivity.this, "删除成功");
                onStaffList();
            }
        });
    }
}
